<template>
  <div class="course-container">
    <!-- 搜索头部 -->
    <div class="search-hd">
      <el-form :mode="searchForm" inline>
        <el-form-item label="课程名称">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入课程名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button :icon="Search" type="primary" @click="search"
            >搜索</el-button
          >
          <el-button :icon="Plus" type="success" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格区域 -->
    <el-table
      :data="tableData"
      style="width: 100%; height: 600px; margin-top: 20px; overflow-y: auto"
      border
    >
      <el-table-column prop="name" label="课程名称" />
      <el-table-column prop="img" label="图片">
        <template #default="scope">
          <el-image :src="scope.row.img"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="introduction" label="课程介绍" />
      <el-table-column prop="previewNum" label="预览人数" />
      <el-table-column prop="totalTime" label="课程时间（分钟）" />
      <el-table-column prop="userId" label="教练" />
      <el-table-column prop="level" label="课程级别">
        <template #default="scope">
          <span>{{ scope.row.sex === 1 ? "零基础" : "高级课程" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="calorie" label="卡路里" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            :icon="Delete"
            type="danger"
            size="small"
            @click="del(scope.row.id)"
            >删除</el-button
          >
          <el-button
            :icon="Edit"
            type="info"
            size="small"
            @click="edit(scope.row.id)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right; margin: 20px"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
      :current-page="pageNum"
      :page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      background
    />
    <!-- 新增编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="新增/编辑" width="500px">
      <el-form :model="courseForm" label-width="80px" ref="ruleFormRef">
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="courseForm.name"></el-input>
        </el-form-item>
        <el-form-item label="课程图片">
          <el-upload
            class="upload-demo"
            list-type="picture-card"
            name="object"
            action="/api/oss/img"
            accept=".jpg,.png"
            :on-remove="handleRemove"
            :limit="1"
            :on-success="onSuccess"
            :on-change="onChange"
            :file-list="fileList"
          >
            <el-icon><plus /></el-icon>
            <template #tip>
              <div class="el-upload__tip">
                jpg/png 格式，小于500KB,只能上传一张图片
              </div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="课程时间" prop="totalTime">
          <el-input v-model="courseForm.totalTime"></el-input>
        </el-form-item>
        <el-form-item label="教练名称" prop="nickName">
          <el-input v-model="courseForm.nickName"></el-input>
        </el-form-item>
        <el-form-item label="课程介绍" prop="introduction">
          <el-input v-model="courseForm.introduction"></el-input>
        </el-form-item>
        <el-form-item label="预览人数" prop="previewNum">
          <el-input v-model="courseForm.previewNum"></el-input>
        </el-form-item>
        <el-form-item label="课程级别" prop="level">
          <el-select
            v-model="courseForm.level"
            class="m-2"
            placeholder="选择级别"
            style="width: 100%"
          >
            <el-option
              v-for="item in levelOptions"
              :key="item.sex"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="卡路里" prop="calorie">
          <el-input v-model="courseForm.calorie"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="submitCourse" type="primary">提交</el-button>
          <el-button @click="cancel">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { Search, Delete, Plus, Edit } from "@element-plus/icons";
import {
  courseList,
  addCourse,
  delCourse,
  queryCourse,
  updateCourse,
} from "@/api/course";
import commonPageRepository from "@/utils/common-page-repository";
import { ElMessage } from "element-plus";
const searchForm = ref({});
const tableData = ref([]);
const pageNum = ref(1);
const pageSize = ref(10);
const totalNum = ref(0);
//图片上传列表
const fileList = ref([]);
function getCourseData() {
  courseList({
    current: pageNum.value,
    size: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.records;
    totalNum.value = res.data.total;
  });
}
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getCourseData
);
// 搜索
function search() {
  pageNum.value = 1;
  courseList({
    current: pageNum.value,
    size: pageSize.value,
    name: searchForm.value.name,
  }).then((res) => {
    ElMessage.success("查询成功");
    tableData.value = res.data.records;
    totalNum.value = res.data.total;
  });
}
const dialogVisible = ref(false);
const courseForm = ref({});
const levelOptions = ref([
  {
    name: "零基础",
    value: 1,
  },
  {
    name: "高级课程",
    value: 2,
  },
]);
// 新增课程
function add() {
  courseId.value = "";
  dialogVisible.value = true;
}
function submitCourse() {
  if (courseId.value) {
    updateCourse({
      id: courseId.value,
      calorie: courseForm.value.calorie,
      introduction: courseForm.value.introduction,
      level: courseForm.value.level,
      name: courseForm.value.name,
      img: courseForm.value.img,
      totalTime: courseForm.value.totalTime,
      nickName: courseForm.value.nickName,
    }).then(() => {
      ElMessage.success("更新成功");
      dialogVisible.value = false;
      ruleFormRef.value.resetFields();
      getCourseData();
      fileList.value = [];
    });
  } else {
    addCourse({
      calorie: courseForm.value.calorie,
      introduction: courseForm.value.introduction,
      level: courseForm.value.level,
      name: courseForm.value.name,
      img: courseForm.value.img,
      totalTime: courseForm.value.totalTime,
      nickName: courseForm.value.nickName,
    }).then(() => {
      ElMessage.success("新增成功");
      dialogVisible.value = false;
      ruleFormRef.value.resetFields();
      getCourseData();
      fileList.value = [];
    });
  }
}
// 删除
function del(id) {
  delCourse({
    ids: id,
  }).then(() => {
    ElMessage.success("删除成功");
    getCourseData();
  });
}
const courseId = ref("");
// 编辑
function edit(id) {
  courseId.value = id;
  dialogVisible.value = true;
  queryCourse(id).then((res) => {
    courseForm.value = res.data;
    fileList.value.push({ url: res.data.img });
  });
}
const ruleFormRef = ref();
// 取消操作
function cancel() {
  ruleFormRef.value.resetFields();
  dialogVisible.value = false;
}
//移除图片回调
function handleRemove(file, files) {
  fileList.value = files;
}
//上传成功时得回调
function onSuccess(file) {
  courseForm.value.img = file.data;
}
//超出图片限制得回调
function onChange(file, files) {
  fileList.value = files;
}
onMounted(() => {
  getCourseData();
});
</script>

<style lang="scss" scoped>
.course-container {
  overflow-y: auto;
}
</style>
